package cn.fj.hrm.controller;

import cn.fj.hrm.service.IEmployeeService;
import cn.fj.hrm.domain.Employee;
import cn.fj.hrm.query.EmployeeQuery;
import cn.fj.hrm.basic.util.JsonResult;
import cn.fj.hrm.basic.util.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;

    /**
    * 保存和修改公用的
    * @param employee  传递的实体
    * @return Ajaxresult转换结果
    */
    @PostMapping("/save")
    public JsonResult save(@RequestBody Employee employee){

            if(employee.getId()!=null){
                //id不为null，就是修改
                employeeService.updateById(employee);
            }else{
                //id为null，就是新增
                employeeService.save(employee);
            }
            return JsonResult.me();

    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){

            employeeService.removeById(id);
            return JsonResult.me();


    }

    /**
    * 获取用户
    * @param id
    */
    @GetMapping("/{id}")
    public Employee get(@PathVariable("id")Long id){
        return employeeService.getById(id);
    }


    /**
    * 查看所有的信息
    * @return
    */
    @GetMapping("/list")
    public List<Employee> list(){
        return employeeService.list(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pagelist")
    public PageList<Employee> json(@RequestBody EmployeeQuery query){
        Page<Employee> page = new Page<Employee>(query.getPageNo(),query.getPageSize());
        page = employeeService.page(page);
        return new PageList<Employee>(page.getTotal(), page.getRecords());
    }
}
